{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from chemview  import MolecularViewer\n",
    "from chemview.trajectory import TrajectoryViewer\n",
    "from chemview.export import display_static"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# We use MDTraj to get a secondary structure\n",
    "import mdtraj as md\n",
    "traj = md.load_pdb('2M6K.pdb')\n",
    "print traj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "top = {}\n",
    "top['atom_types'] = [a.element.symbol for a in traj.topology.atoms]\n",
    "top['atom_names'] = [a.name for a in traj.topology.atoms]\n",
    "top['bonds'] = [(a.index, b.index) for a, b in traj.topology.bonds]\n",
    "top['secondary_structure'] = md.compute_dssp(traj[0])[0]\n",
    "top['residue_types'] = [r.name for r in traj.topology.residues ]\n",
    "top['residue_indices'] = [ [a.index for a in r.atoms] for r in traj.topology.residues ]\n",
    "\n",
    "mv = MolecularViewer(traj.xyz[0], top)\n",
    "mv.cylinder_and_strand()\n",
    "mv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Display static\n",
    "display_static(mv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "def callback(content):\n",
    "    with open('data.json', 'wb') as fp:\n",
    "        json.dump(content['json'], fp)\n",
    "\n",
    "mv._connect_event('serialize', callback)\n",
    "mv._remote_call('_handle_serialize')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%%javascript\n",
    "\n",
    "var prefix = \"https://rawgit.com/gabrielelanaro/chemview/master/chemview/static/\";\n",
    "\n",
    "require.config({\n",
    "    paths: {\n",
    "        'jquery': prefix + 'jquery.min',\n",
    "        'jqueryui': prefix + 'jquery-ui.min',\n",
    "        'exporter': prefix + 'objexporter.js',\n",
    "        'three': prefix + 'three.min',\n",
    "        'base64-arraybuffer': prefix + 'base64-arraybuffer',\n",
    "        'ArcballControls' : prefix + 'ArcballControls',\n",
    "        'chemview': prefix + 'chemview',\n",
    "    },\n",
    "    shim: {\n",
    "        three: {\n",
    "            exports: 'THREE'\n",
    "        },\n",
    "\n",
    "        chemview: {\n",
    "            deps: ['three', 'ArcballControls', 'base64-arraybuffer'],\n",
    "            exports: 'MolecularViewer'\n",
    "        },\n",
    "\n",
    "        ArcballControls: {\n",
    "            deps: ['three'],\n",
    "            exports: 'THREE.ArcballControls',\n",
    "        },\n",
    "    },\n",
    "});"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Serialize this thing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%%html\n",
    "\n",
    "<canvas id=\"molecular_viewer\"></canvas>\n",
    "<script>\n",
    "\n",
    "require(['chemview'], function () {\n",
    "    console.log('done loading');\n",
    "    var canvas = $(\"#molecular_viewer\").css({width: 400, height: 400});\n",
    "    var mv = new MolecularViewer(canvas);\n",
    "    var prefix = \"https://rawgit.com/gabrielelanaro/chemview/master/\";\n",
    "\n",
    "    var datafile = prefix + 'docs/source/_static/protein_rendered.json';\n",
    "\n",
    "    $.getJSON(datafile, function (data){\n",
    "        mv.deserialize(data);\n",
    "        mv.animate();\n",
    "\n",
    "    // Give it a nice zoom\n",
    "        mv.controls.dollyIn(1.9);\n",
    "    });\n",
    "\n",
    "    mv.resize(canvas.width(), canvas.height());\n",
    "});\n",
    "\n",
    "</script>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
